package iva;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import com.swtdesigner.SWTResourceManager;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class BuscarProveedor {
	protected Shell shlBuscarProveedor;
	private Text txtIngreseElNombre;
	private Composite composite;
	private Composite composite_2;
	private Composite composite_1;
	private Table table;
	private String codigoProveedor;
	private String nombreProveedor;
	
	
	public String getNombreProveedor() {
		return nombreProveedor;
	}

	public void setNombreProveedor(String nombreProveedor) {
		this.nombreProveedor = nombreProveedor;
	}

	public String getCodigoProveedor() {
		return codigoProveedor;
	}

	public void setCodigoProveedor(String codigoProveedor) {
		this.codigoProveedor = codigoProveedor;
	}

	/**
	 * Launch the application.
	 * @param args
	 */
	
	
	public void buscarDatos(String busqueda){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/addisyc", "", ""); 
			String query="SELECT DISTINCT codigo, nombre, cuit FROM proveedo WHERE nombre LIKE '%"+busqueda+"%' order by codigo asc";
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.execute();
			
			ResultSet rs = ps.getResultSet();
			while (rs.next()){
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(0, rs.getString(1));
				item.setText(1, rs.getString(2));
				item.setText(2, rs.getString(3));
				
			}
			ps.close();
			conexion.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void buscarDatos(long busqueda){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/addisyc", "", ""); 
			String query="SELECT DISTINCT codigo, nombre, cuit FROM proveedo WHERE cuit = (SELECT INSERT(INSERT('"+busqueda+"',3,0,'-'),12,0,'-'))";
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.execute();
			
			ResultSet rs = ps.getResultSet();
			while (rs.next()){
				TableItem item = new TableItem(table, SWT.NONE);
				item.setText(0, rs.getString(1));
				item.setText(1, rs.getString(2));
				item.setText(2, rs.getString(3));
				
			}
			ps.close();
			conexion.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					BuscarProveedor window = new BuscarProveedor();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shlBuscarProveedor.open();
		shlBuscarProveedor.layout();
		while (!shlBuscarProveedor.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	
	
	
	
	
	protected void createContents() {
		shlBuscarProveedor = new Shell(SWT.CLOSE | SWT.MIN);
		shlBuscarProveedor.setSize(576, 422);
		//shell.setSize(754, 538);
		shlBuscarProveedor.setImage(SWTResourceManager.getImage(BuscarProveedor.class, "/imagenes/AppIcon.png"));
		shlBuscarProveedor.setText("Buscar Proveedor");
		shlBuscarProveedor.setLayout(new FormLayout());
		

		Rectangle bds = Display.getCurrent().getClientArea();
        Point p = shlBuscarProveedor.getSize();

        int nLeft = (bds.width - p.x) / 2;
        int nTop = (bds.height - p.y) / 2;

        shlBuscarProveedor.setBounds(nLeft, nTop, p.x, p.y);
		
		
		
		composite = new Composite(shlBuscarProveedor, SWT.BORDER);
		FormData fd_composite = new FormData();
		fd_composite.left = new FormAttachment(0, 10);
		composite.setLayoutData(fd_composite);
		composite.setLayout(new GridLayout(3, false));
		
		composite_2 = new Composite(shlBuscarProveedor, SWT.BORDER);
		fd_composite.right = new FormAttachment(composite_2, 0, SWT.RIGHT);
		fd_composite.bottom = new FormAttachment(composite_2, -6);
		fd_composite.top = new FormAttachment(0, 10);
		composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));
		FormData fd_composite_2 = new FormData();
		fd_composite_2.top = new FormAttachment(0, 62);
		fd_composite_2.left = new FormAttachment(0, 10);
		fd_composite_2.right = new FormAttachment(100, -10);
		composite_2.setLayoutData(fd_composite_2);
		
		composite_1 = new Composite(shlBuscarProveedor, SWT.NONE);
		fd_composite_2.bottom = new FormAttachment(composite_1, -6);
		composite_1.setLayout(new FormLayout());
		FormData fd_composite_1 = new FormData();
		fd_composite_1.left = new FormAttachment(0, 10);
		fd_composite_1.right = new FormAttachment(100, -10);
		fd_composite_1.top = new FormAttachment(0, 332);
		fd_composite_1.bottom = new FormAttachment(100, -10);
		
		table = new Table(composite_2, SWT.BORDER | SWT.FULL_SELECTION);
		table.addMouseListener(new MouseAdapter() {
			public void mouseDoubleClick(MouseEvent e) {
				try {
					TableItem item = table.getSelection()[0];
					setCodigoProveedor(item.getText(0).toString());
					setNombreProveedor(item.getText(1).toString());
					shlBuscarProveedor.close();
				} catch (Exception ep) {
					ep.printStackTrace();
				}

			}
		});
		

		table.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.NORMAL));
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnProveedor = new TableColumn(table, SWT.NONE);
		tblclmnProveedor.setWidth(71);
		tblclmnProveedor.setText("C\u00F3digo");
		
		TableColumn tblclmnFecha = new TableColumn(table, SWT.NONE);
		tblclmnFecha.setWidth(268);
		tblclmnFecha.setText("Nombre");
		
		TableColumn tblclmnFactura = new TableColumn(table, SWT.NONE);
		tblclmnFactura.setWidth(145);
		tblclmnFactura.setText("CUIT");
		
		Menu menu = new Menu(table);
		table.setMenu(menu);
		final Clipboard cb = new Clipboard(Display.getDefault());

		

		
		MenuItem mntmCopiar = new MenuItem(menu, SWT.NONE);
		mntmCopiar.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try {
					TableItem item = table.getSelection()[0];
					if (!item.getText(0).isEmpty()){
						String textData = item.getText(0);
						if (textData.length() > 0) {
							TextTransfer textTransfer = TextTransfer.getInstance();
							cb.setContents(new Object[]{textData}, new Transfer[]{textTransfer});
						}
					}
					
					
					
				} catch (Exception e2) {
					// TODO: handle exception
				}
			}
		});
		mntmCopiar.setText("Copiar");
		
		
		composite_1.setLayoutData(fd_composite_1);
		
		Button btnSalir = new Button(composite_1, SWT.NONE);
		btnSalir.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				shlBuscarProveedor.close();
			}
		});
		btnSalir.setAlignment(SWT.RIGHT);
		btnSalir.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		btnSalir.setImage(SWTResourceManager.getImage(BuscarProveedor.class, "/imagenes/delete-icon.png"));
		FormData fd_btnSalir = new FormData();
		fd_btnSalir.top = new FormAttachment(0, 7);
		fd_btnSalir.right = new FormAttachment(100);
		fd_btnSalir.bottom = new FormAttachment(100, -8);
		fd_btnSalir.left = new FormAttachment(100, -107);
		btnSalir.setLayoutData(fd_btnSalir);
		btnSalir.setText("Salir");
		
		Label lblProveedor = new Label(composite, SWT.NONE);
		lblProveedor.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblProveedor.setText("Proveedor:");
		lblProveedor.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		
		txtIngreseElNombre = new Text(composite, SWT.BORDER);
		txtIngreseElNombre.addFocusListener(new FocusAdapter() {
			public void focusGained(FocusEvent e) {
				txtIngreseElNombre.setText("");
			}
		});
		txtIngreseElNombre.setForeground(SWTResourceManager.getColor(0, 0, 0));
		txtIngreseElNombre.setText("Ingrese parte del nombre o cuit (sin guiones) .");
		txtIngreseElNombre.setFont(SWTResourceManager.getFont("Calibri", 11, SWT.BOLD));
		GridData gd_txtIngreseElNombre = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_txtIngreseElNombre.widthHint = 355;
		gd_txtIngreseElNombre.heightHint = 22;
		txtIngreseElNombre.setLayoutData(gd_txtIngreseElNombre);
		
		Button buscarButton = new Button(composite, SWT.NONE);
		buscarButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				table.removeAll();
				if (txtIngreseElNombre.getText().startsWith("2") || (txtIngreseElNombre.getText().startsWith("3"))) 
					buscarDatos(Long.parseLong(txtIngreseElNombre.getText()));
				else
					buscarDatos(txtIngreseElNombre.getText());
				table.setFocus();
			}
		});
		GridData gd_buscarButton = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_buscarButton.heightHint = 32;
		gd_buscarButton.widthHint = 85;
		buscarButton.setLayoutData(gd_buscarButton);
		buscarButton.setText("Buscar");
		
		shlBuscarProveedor.setDefaultButton(buscarButton);
		composite.setTabList(new Control[]{txtIngreseElNombre, buscarButton});

		

		
	}
}
